package Key_Value类型

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object groupByKey {
    //groupByKey也是对每个key进行操作，但只生成一个sequence。
    def main(args: Array[String]): Unit = {

        val config: SparkConf = new SparkConf().setMaster("local[*]").setAppName("WordCount")

        val sc = new SparkContext(config)

        val words = Array("one", "two", "two", "three", "three", "three")

        val wordPairsRDD = sc.parallelize(words).map(word => (word, 1))

        //将相同key对应值聚合到一个sequence中
        val group = wordPairsRDD.groupByKey()

        group.collect().foreach(println)

        //计算相同key对应值的相加结果
        val res: RDD[(String, Int)] = group.map(t => (t._1, t._2.sum))

        res.collect().foreach(println)
    }

}
